package com.zzw.thread;

import java.util.concurrent.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThreadPoolExcutorTest {

	private static Logger logger = LoggerFactory.getLogger(ThreadPoolExcutorTest.class);
	
	public void run(){
		ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS,
				new ArrayBlockingQueue<Runnable>(1),new RejectedExecutionHandler() {
			@Override
			public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
				logger.error("reject submit thread to thread pool");
			}
		});
		
		for(int i = 0;i < 20;i++){
			executor.submit(new Runnable() {
				@Override
				public void run() {
					try {
						Thread.sleep(100000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName());
				}
			});
		}
		
		for(int i = 0;i < 5;i++){
			Future<String> task = executor.submit(new Callable<String>() {
				@Override
				public String call() throws Exception {
					return "success";
				}
			});
			String res = "";
			try {
				res = task.get();
			} catch (Exception e) {
				logger.error("get result error",e);
				e.printStackTrace();
			}
			System.out.println(res);
		}
	}

	public static void main(String[] args) {
		ThreadPoolExcutorTest test = new ThreadPoolExcutorTest();
		test.run();
	}
}